home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / bfd / coff-h8500.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  9.8 KB  |  356 lines

  1. /* BFD back-end for Hitachi H8/500 COFF binaries.
  2.    Copyright 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.    Written by Steve Chamberlain, <sac@cygnus.com>.
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include "obstack.h"
  25. #include "libbfd.h"
  26. #include "bfdlink.h"
  27. #include "coff/h8500.h"
  28. #include "coff/internal.h"
  29. #include "libcoff.h"
  30.  
  31. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
  32.  
  33. static reloc_howto_type r_imm8 =
  34. HOWTO (R_H8500_IMM8, 0, 1, 8, false, 0,
  35.        complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff, false);
  36.  
  37. static reloc_howto_type r_imm16 =
  38. HOWTO (R_H8500_IMM16, 0, 1, 16, false, 0,
  39.        complain_overflow_bitfield, 0, "r_imm16", true, 0x0000ffff, 0x0000ffff, false);
  40.  
  41. static reloc_howto_type r_imm24 =
  42. HOWTO (R_H8500_IMM24, 0, 1, 24, false, 0,
  43.        complain_overflow_bitfield, 0, "r_imm24", true, 0x00ffffff, 0x00ffffff, false);
  44.  
  45. static reloc_howto_type r_imm32 =
  46. HOWTO (R_H8500_IMM32, 0, 1, 32, false, 0,
  47.        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false);
  48.  
  49.  
  50. static reloc_howto_type r_high8 =
  51. HOWTO (R_H8500_HIGH8, 0, 1, 8, false, 0,
  52.        complain_overflow_dont, 0, "r_high8", true, 0x000000ff, 0x000000ff, false);
  53.  
  54. static reloc_howto_type r_low16 =
  55. HOWTO (R_H8500_LOW16, 0, 1, 16, false, 0,
  56.        complain_overflow_dont, 0, "r_low16", true, 0x0000ffff, 0x0000ffff, false);
  57.  
  58. static reloc_howto_type r_pcrel8 =
  59. HOWTO (R_H8500_PCREL8, 0, 1, 8, true, 0, complain_overflow_signed, 0, "r_pcrel8", true, 0, 0, true);
  60.  
  61.  
  62. static reloc_howto_type r_pcrel16 =
  63. HOWTO (R_H8500_PCREL16, 0, 1, 16, true, 0, complain_overflow_signed, 0, "r_pcrel16", true, 0, 0, true);
  64.  
  65. static reloc_howto_type r_high16 =
  66. HOWTO (R_H8500_HIGH16, 0, 1, 8, false, 0,
  67.        complain_overflow_dont, 0, "r_high16", true, 0x000ffff, 0x0000ffff, false);
  68.  
  69.  
  70. /* Turn a howto into a reloc number */
  71.  
  72. static int 
  73. coff_h8500_select_reloc (howto)
  74.      reloc_howto_type *howto;
  75. {
  76.   return howto->type;
  77. }
  78.  
  79. #define SELECT_RELOC(x,howto) x.r_type = coff_h8500_select_reloc(howto)
  80.  
  81.  
  82. #define BADMAG(x) H8500BADMAG(x)
  83. #define H8500 1            /* Customize coffcode.h */
  84.  
  85. #define __A_MAGIC_SET__
  86.  
  87. /* Code to swap in the reloc */
  88. #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
  89. #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
  90. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  91.   dst->r_stuff[0] = 'S'; \
  92.   dst->r_stuff[1] = 'C';
  93.  
  94. /* Code to turn a r_type into a howto ptr, uses the above howto table
  95.    */
  96.  
  97. static void
  98. rtype2howto(internal, dst)
  99.      arelent * internal;
  100.      struct internal_reloc *dst;
  101. {
  102.   switch (dst->r_type)
  103.     {
  104.     default:
  105.       abort ();
  106.       break;
  107.     case R_H8500_IMM8:
  108.       internal->howto = &r_imm8;
  109.       break;
  110.     case R_H8500_IMM16:
  111.       internal->howto = &r_imm16;
  112.       break;
  113.     case R_H8500_IMM24:
  114.       internal->howto = &r_imm24;
  115.       break;
  116.     case R_H8500_IMM32:
  117.       internal->howto = &r_imm32;
  118.       break;
  119.     case R_H8500_PCREL8:
  120.       internal->howto = &r_pcrel8;
  121.       break;
  122.     case R_H8500_PCREL16:
  123.       internal->howto = &r_pcrel16;
  124.       break;
  125.     case R_H8500_HIGH8:
  126.       internal->howto = &r_high8;
  127.       break;
  128.     case R_H8500_HIGH16:
  129.       internal->howto = &r_high16;
  130.       break;
  131.     case R_H8500_LOW16:
  132.       internal->howto = &r_low16;
  133.       break;
  134.     }
  135. }
  136.  
  137. #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
  138.  
  139.  
  140. /* Perform any necessaru magic to the addend in a reloc entry */
  141.  
  142.  
  143. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  144.  cache_ptr->addend =  ext_reloc.r_offset;
  145.  
  146.  
  147. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  148.  reloc_processing(relent, reloc, symbols, abfd, section)
  149.  
  150. static void reloc_processing (relent, reloc, symbols, abfd, section)
  151.      arelent * relent;
  152.      struct internal_reloc *reloc;
  153.      asymbol ** symbols;
  154.      bfd * abfd;
  155.      asection * section;
  156. {
  157.   relent->address = reloc->r_vaddr;
  158.   rtype2howto (relent, reloc);
  159.  
  160.   if (reloc->r_symndx > 0)
  161.     {
  162.       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
  163.     }
  164.   else
  165.     {
  166.       relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  167.     }
  168.  
  169.  
  170.   relent->addend = reloc->r_offset;
  171.   relent->address -= section->vma;
  172. }
  173.  
  174. static void
  175. extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
  176.      bfd *in_abfd;
  177.      struct bfd_link_info *link_info;
  178.      struct bfd_link_order *link_order;
  179.      arelent *reloc;
  180.      bfd_byte *data;
  181.      unsigned int *src_ptr;
  182.      unsigned int *dst_ptr;
  183. {
  184.   bfd_byte *d = data+*dst_ptr;
  185.   asection *input_section = link_order->u.indirect.section;
  186.   switch (reloc->howto->type)
  187.     {
  188.     case R_H8500_IMM8:
  189.       bfd_put_8 (in_abfd,
  190.          bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  191.          d);
  192.       (*dst_ptr) += 1;
  193.       (*src_ptr) += 1;
  194.       break;
  195.  
  196.     case R_H8500_HIGH8:
  197.       bfd_put_8 (in_abfd,
  198.          (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
  199.           >> 16),
  200.          d );
  201.       (*dst_ptr) += 1;
  202.       (*src_ptr) += 1;
  203.       break;
  204.  
  205.     case R_H8500_IMM16:
  206.       bfd_put_16 (in_abfd,
  207.           bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  208.           d  );
  209.       (*dst_ptr) += 2;
  210.       (*src_ptr) += 2;
  211.       break;
  212.  
  213.     case R_H8500_LOW16:
  214.       bfd_put_16 (in_abfd,
  215.           bfd_coff_reloc16_get_value (reloc, link_info, input_section),
  216.           d);
  217.  
  218.       (*dst_ptr) += 2;
  219.       (*src_ptr) += 2;
  220.       break;
  221.       
  222.     case R_H8500_HIGH16:
  223.       bfd_put_16 (in_abfd,
  224.           (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
  225.            >>16),
  226.           d);
  227.  
  228.       (*dst_ptr) += 2;
  229.       (*src_ptr) += 2;
  230.       break;
  231.  
  232.     case R_H8500_IMM24:
  233.       {
  234.     int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
  235.     int o = bfd_get_32(in_abfd, data+ *dst_ptr -1);
  236.     v = (v & 0x00ffffff) | (o & 0xff00000);
  237.     bfd_put_32 (in_abfd, v, data  + *dst_ptr -1);
  238.     (*dst_ptr) +=3;
  239.     (*src_ptr)+=3;;
  240.       }
  241.       break;
  242.     case R_H8500_IMM32:
  243.       {
  244.     int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
  245.     bfd_put_32 (in_abfd, v, data  + *dst_ptr);
  246.     (*dst_ptr) +=4;
  247.     (*src_ptr)+=4;;
  248.       }
  249.       break;
  250.  
  251.  
  252.     case R_H8500_PCREL8:
  253.       {
  254.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
  255.                           input_section);
  256.     bfd_vma dot = link_order->offset
  257.       + *dst_ptr
  258.         + link_order->u.indirect.section->output_section->vma;
  259.     int gap = dst - dot - 1; /* -1 since were in the odd byte of the
  260.                     word and the pc's been incremented */
  261.  
  262.     if (gap > 128 || gap < -128)
  263.       {
  264.         if (! ((*link_info->callbacks->reloc_overflow)
  265.            (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
  266.             reloc->howto->name, reloc->addend, input_section->owner,
  267.             input_section, reloc->address)))
  268.           abort ();
  269.       }
  270.     bfd_put_8 (in_abfd, gap, data + *dst_ptr);
  271.     (*dst_ptr)++;
  272.     (*src_ptr)++;
  273.     break;
  274.       }
  275.     case R_H8500_PCREL16:
  276.       {
  277.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
  278.                           input_section);
  279.     bfd_vma dot = link_order->offset
  280.       + *dst_ptr
  281.         + link_order->u.indirect.section->output_section->vma;
  282.     int gap = dst - dot - 1; /* -1 since were in the odd byte of the
  283.                     word and the pc's been incremented */
  284.  
  285.     if (gap > 32767 || gap < -32768)
  286.       {
  287.         if (! ((*link_info->callbacks->reloc_overflow)
  288.            (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
  289.             reloc->howto->name, reloc->addend, input_section->owner,
  290.             input_section, reloc->address)))
  291.           abort ();
  292.       }
  293.     bfd_put_16 (in_abfd, gap, data + *dst_ptr);
  294.     (*dst_ptr)+=2;
  295.     (*src_ptr)+=2;
  296.     break;
  297.       }
  298.  
  299.     default:
  300.       abort ();
  301.     }
  302. }
  303.  
  304. #define coff_reloc16_extra_cases extra_case
  305.  
  306. #include "coffcode.h"
  307.  
  308.  
  309. #undef  coff_bfd_get_relocated_section_contents
  310. #undef coff_bfd_relax_section
  311. #define coff_bfd_get_relocated_section_contents \
  312.   bfd_coff_reloc16_get_relocated_section_contents
  313. #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
  314.  
  315. const bfd_target h8500coff_vec =
  316. {
  317.   "coff-h8500",            /* name */
  318.   bfd_target_coff_flavour,
  319.   BFD_ENDIAN_BIG,        /* data byte order is big */
  320.   BFD_ENDIAN_BIG,        /* header byte order is big */
  321.  
  322.   (HAS_RELOC | EXEC_P |        /* object flags */
  323.    HAS_LINENO | HAS_DEBUG |
  324.    HAS_SYMS | HAS_LOCALS | WP_TEXT),
  325.  
  326.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  327.   '_',                /* leading symbol underscore */
  328.   '/',                /* ar_pad_char */
  329.   15,                /* ar_max_namelen */
  330.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  331.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  332.      bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* data */
  333.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  334.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  335.      bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* hdrs */
  336.  
  337.   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
  338.    bfd_generic_archive_p, _bfd_dummy_target},
  339.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
  340.    bfd_false},
  341.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  342.    _bfd_write_archive_contents, bfd_false},
  343.  
  344.      BFD_JUMP_TABLE_GENERIC (coff),
  345.      BFD_JUMP_TABLE_COPY (coff),
  346.      BFD_JUMP_TABLE_CORE (_bfd_nocore),
  347.      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  348.      BFD_JUMP_TABLE_SYMBOLS (coff),
  349.      BFD_JUMP_TABLE_RELOCS (coff),
  350.      BFD_JUMP_TABLE_WRITE (coff),
  351.      BFD_JUMP_TABLE_LINK (coff),
  352.      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  353.  
  354.   COFF_SWAP_TABLE,
  355. };
  356.